/**
 * 用户数据工具类
 */

// 验证用户数据完整性
export const validateUserData = (userData) => {
  if (!userData) {
    return {
      username: '用户',
      phone: '',
      email: '',
      address: '',
      isValid: false
    }
  }

  return {
    username: userData.username || '用户',
    phone: userData.phone || '',
    email: userData.email || '',
    address: userData.address || '',
    isValid: true,
    ...userData
  }
}

// 格式化用户信息用于提交
export const formatUserInfoForSubmit = (userInfo) => {
  return {
    username: userInfo.username?.trim() || '',
    phone: userInfo.phone?.trim() || '',
    email: userInfo.email?.trim() || '',
    address: userInfo.address?.trim() || ''
  }
}

// 验证用户信息
export const validateUserInfo = (userInfo) => {
  const errors = []
  
  if (!userInfo.username?.trim()) {
    errors.push('用户名不能为空')
  }
  
  if (userInfo.phone && !/^1[3-9]\d{9}$/.test(userInfo.phone)) {
    errors.push('请输入正确的手机号码')
  }
  
  if (userInfo.email && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(userInfo.email)) {
    errors.push('请输入正确的邮箱地址')
  }
  
  if (!userInfo.address?.trim()) {
    errors.push('地址不能为空')
  }
  
  return {
    isValid: errors.length === 0,
    errors
  }
}

// 处理API响应中的用户数据
export const processUserApiResponse = (response) => {
  try {
    if (!response || !response.data) {
      return validateUserData(null)
    }
    
    return validateUserData(response.data)
  } catch (error) {
    console.error('处理用户数据失败:', error)
    return validateUserData(null)
  }
} 